package com.hzdl.car.contorlle;

import com.github.pagehelper.PageInfo;
import com.hzdl.car.bean.Customer;
import com.hzdl.car.service.CustomerService;
import com.hzdl.car.util.Common;
import com.hzdl.car.util.HzdlResultJson;
import com.hzdl.car.vo.CustomerVo;
import com.hzdl.car.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.util.List;

@RestController
@RequestMapping("customer")
public class CustomerControlle {
    @Autowired
    private CustomerService service;
    @RequestMapping("init")
    public HzdlResultJson init(CustomerVo customerVo){
        PageInfo<Customer>pageInfo = service.findWhere(customerVo);
        return new HzdlResultJson(pageInfo.getTotal(),pageInfo.getList());
    }
    @RequestMapping("add")
    public HzdlResultJson add(CustomerVo customerVo){
        int row = service.add(customerVo);
        if (row>0){
            return new HzdlResultJson("添加成功",null);
        }else {
            return new HzdlResultJson(-1,"添加失败");
        }
    }
    @RequestMapping("update")
    public HzdlResultJson update(CustomerVo customerVo){
        int row = service.update(customerVo);
        if (row>0){
            return new HzdlResultJson("修改成功",null);
        }
        return new HzdlResultJson(-1,"修改失败");

    }

    @RequestMapping("delete")
    public HzdlResultJson delete ( CustomerVo customerVo){
        int row = service.delete(customerVo.getIdentity());
        if (row>0){
            return HzdlResultJson.DELETE_SUCCESS();
        }else{
            return HzdlResultJson.DELETE_FAIL();
        }
    }

    @RequestMapping("deleteAll")
    @ResponseBody
    public HzdlResultJson deleteAll ( CustomerVo customerVo,@RequestParam(value = "ids") String ids){
        int row =0;
        String[] split = ids.split(",");

        for(String  id:split){
            row+=service.delete(id);
        }

        if (row>0){
            return HzdlResultJson.DELETE_ALL_SUCCESS();
        }else{
            return HzdlResultJson.DELETE_ALL_FAIL();
        }
    }

    @RequestMapping("export")
    public ResponseEntity<Object> export(CustomerVo customerVo){
        List<Customer> list = service.findAll(customerVo);
        ByteArrayOutputStream outputStream = Common.print(list);
        HttpHeaders headers=new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment","customers.xls");

        return new ResponseEntity<Object>(outputStream.toByteArray(),headers, HttpStatus.CREATED);
    }



}
